// Knockout Mapping plugin v1.0
// (c) 2011 Steven Sanderson, Roy Jacobs - http://knockoutjs.com/
// License: Ms-Pl (http://www.opensource.org/licenses/ms-pl.html)

ko.exportSymbol=function(j,o){for(var i=j.split("."),q=window,k=0;k<i.length-1;k++)q=q[i[k]];q[i[i.length-1]]=o};ko.exportProperty=function(j,o,i){j[o]=i};
(function(){function j(a,c){for(var b in c)c.hasOwnProperty(b)&&c[b]&&(a[b]=c[b])}function o(a,c){var b={};j(b,a);j(b,c);return b}function i(a){if(a&&typeof a==="object"&&a.constructor==(new Date).constructor)return"date";return typeof a}function q(){ko.dependentObservable=function(a,c,b){b=b||{};b.deferEvaluation=!0;a=new w(a,c,b);a.__ko_proto__=w;return a}}function k(a,c,b,d,e,h){var t=ko.utils.unwrapObservable(c)instanceof Array;if(ko.mapping.isMapped(a))var g=ko.utils.unwrapObservable(a)[l],b=
o(g,b);g=function(){return b[e]&&b[e].create instanceof Function};d=d||new z;if(d.get(c))return a;e=e||"";if(t){var t=[],f=function(a){return a};if(b[e]&&b[e].key)f=b[e].key;var j=function(a){return a};g()&&(j=function(a){return b[e].create({data:a,parent:h})});if(!ko.isObservable(a))a=ko.observableArray([]),a.mappedRemove=function(b){var c=typeof b=="function"?b:function(a){return a===f(b)};return a.remove(function(a){return c(f(a))})},a.mappedRemoveAll=function(b){var c=r(b,f);return a.remove(function(a){return ko.utils.arrayIndexOf(c,
f(a))!=-1})},a.mappedDestroy=function(b){var c=typeof b=="function"?b:function(a){return a===f(b)};return a.destroy(function(a){return c(f(a))})},a.mappedDestroyAll=function(b){var c=r(b,f);return a.destroy(function(a){return ko.utils.arrayIndexOf(c,f(a))!=-1})},a.mappedIndexOf=function(b){var c=r(a(),f),b=f(b);return ko.utils.arrayIndexOf(c,b)},a.mappedCreate=function(b){if(a.mappedIndexOf(b)!==-1)throw Error("There already is an object with the key that you specified.");b=j(b);a.push(b);return b};
for(var g=r(ko.utils.unwrapObservable(a),f).sort(),s=r(c,f).sort(),g=ko.utils.compareArrays(g,s),s=[],x=0,v=g.length;x<v;x++){var p=g[x],m;switch(p.status){case "added":var n=u(ko.utils.unwrapObservable(c),p.value,f);m=ko.utils.unwrapObservable(k(void 0,n,b,d,e,a));n=ko.utils.arrayIndexOf(ko.utils.unwrapObservable(c),n);s[n]=m;break;case "retained":n=u(ko.utils.unwrapObservable(c),p.value,f);m=u(a,p.value,f);k(m,n,b,d,e,a);n=ko.utils.arrayIndexOf(ko.utils.unwrapObservable(c),n);s[n]=m;break;case "deleted":m=
u(a,p.value,f)}t.push({event:p.status,item:m})}a(s);b[e]&&b[e].arrayChanged&&ko.utils.arrayForEach(t,function(a){b[e].arrayChanged(a.event,a.item)})}else if(y(c)){if(!a)if(g())return q(),m=b[e].create({data:c,parent:h}),ko.dependentObservable=w,m;else a={};d.save(c,a);A(c,function(f){var g=d.get(c[f]);a[f]=g?g:k(a[f],c[f],b,d,f,a);b.mappedProperties[B(e,c,f)]=!0})}else switch(i(c)){case "function":a=c;break;default:ko.isWriteableObservable(a)?a(ko.utils.unwrapObservable(c)):a=ko.observable(ko.utils.unwrapObservable(c))}return a}
function v(a,c){var b;c&&(b=c(a));b||(b=a);return ko.utils.unwrapObservable(b)}function u(a,c,b){a=ko.utils.arrayFilter(ko.utils.unwrapObservable(a),function(a){return v(a,b)==c});if(a.length==0)throw Error("When calling ko.update*, the key '"+c+"' was not found!");if(a.length>1&&y(a[0]))throw Error("When calling ko.update*, the key '"+c+"' was not unique!");return a[0]}function r(a,c){return ko.utils.arrayMap(ko.utils.unwrapObservable(a),function(a){return c?v(a,c):a})}function A(a,c){if(a instanceof
Array)for(var b=0;b<a.length;b++)c(b);else for(b in a)c(b)}function y(a){return i(a)=="object"&&a!==null&&a!==void 0}function B(a,c,b){var d=a||"";c instanceof Array?a&&(d+="["+b+"]"):(a&&(d+="."),d+=b);return d}function z(){var a=[],c=[];this.save=function(b,d){var e=ko.utils.arrayIndexOf(a,b);e>=0?c[e]=d:(a.push(b),c.push(d))};this.get=function(b){b=ko.utils.arrayIndexOf(a,b);return b>=0?c[b]:void 0}}ko.mapping={};var l="__ko_mapping__",w=ko.dependentObservable,h;ko.mapping.fromJS=function(a,c,
b){if(arguments.length==0)throw Error("When calling ko.fromJS, pass the object you want to convert.");var d;d=c||{};if(d.create instanceof Function||d.key instanceof Function||d.arrayChanged instanceof Function)d={"":d};d.mappedProperties={};c=d;d=k(b,a,c);d[l]=o(d[l],c);return d};ko.mapping.fromJSON=function(a,c){var b=ko.utils.parseJson(a);return ko.mapping.fromJS(b,c)};ko.mapping.isMapped=function(a){return(a=ko.utils.unwrapObservable(a))&&a[l]};ko.mapping.updateFromJS=function(a,c){if(arguments.length<
2)throw Error("When calling ko.updateFromJS, pass: the object to update and the object you want to update from.");if(!a)throw Error("The object is undefined.");if(!a[l])throw Error("The object you are trying to update was not created by a 'fromJS' or 'fromJSON' mapping.");return k(a,c,a[l])};ko.mapping.updateFromJSON=function(a,c,b){c=ko.utils.parseJson(c);return ko.mapping.updateFromJS(a,c,b)};ko.mapping.toJS=function(a,c){h||ko.mapping.resetDefaultOptions();if(arguments.length==0)throw Error("When calling ko.mapping.toJS, pass the object you want to convert.");
if(!(h.ignore instanceof Array))throw Error("ko.mapping.defaultOptions().ignore should be an array.");if(!(h.include instanceof Array))throw Error("ko.mapping.defaultOptions().include should be an array.");c=c||{};if(!(c.ignore instanceof Array))c.ignore=[c.ignore];c.ignore=c.ignore.concat(h.ignore);if(!(c.include instanceof Array))c.include=[c.include];c.include=c.include.concat(h.include);return ko.mapping.visitModel(a,function(a){return ko.utils.unwrapObservable(a)},c)};ko.mapping.toJSON=function(a,
c){var b=ko.mapping.toJS(a,c);return ko.utils.stringifyJson(b)};ko.mapping.defaultOptions=function(){if(arguments.length>0)h=arguments[0];else return h};ko.mapping.resetDefaultOptions=function(){h={include:["_destroy"],ignore:[]}};ko.mapping.visitModel=function(a,c,b){b=b||{};b.visitedObjects=b.visitedObjects||new z;var d,e=ko.utils.unwrapObservable(a);if(y(e))c(a,b.parentName),d=e instanceof Array?[]:{};else return c(a,b.parentName);b.visitedObjects.save(a,d);var h=b.parentName;A(e,function(a){if(!(b.ignore&&
ko.utils.arrayIndexOf(b.ignore,a)!=-1)){var g=e[a];b.parentName=B(h,e,a);if(!(b.include&&ko.utils.arrayIndexOf(b.include,a)===-1)||!e[l]||!e[l].mappedProperties||e[l].mappedProperties[a]||e instanceof Array)switch(i(ko.utils.unwrapObservable(g))){case "object":case "undefined":var f=b.visitedObjects.get(g);d[a]=f!==void 0?f:ko.mapping.visitModel(g,c,b);break;default:d[a]=c(g,b.parentName)}}});return d};ko.exportSymbol("ko.mapping",ko.mapping);ko.exportSymbol("ko.mapping.fromJS",ko.mapping.fromJS);
ko.exportSymbol("ko.mapping.fromJSON",ko.mapping.fromJSON);ko.exportSymbol("ko.mapping.isMapped",ko.mapping.isMapped);ko.exportSymbol("ko.mapping.defaultOptions",ko.mapping.defaultOptions);ko.exportSymbol("ko.mapping.toJS",ko.mapping.toJS);ko.exportSymbol("ko.mapping.toJSON",ko.mapping.toJSON);ko.exportSymbol("ko.mapping.updateFromJS",ko.mapping.updateFromJS);ko.exportSymbol("ko.mapping.updateFromJSON",ko.mapping.updateFromJSON);ko.exportSymbol("ko.mapping.visitModel",ko.mapping.visitModel)})();
